﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility1;

namespace Thv.trangchu.page
{
    public partial class trangchu : System.Web.UI.Page
    {
        DataClasses1DataContext dx = new DataClasses1DataContext();
        protected string templatePath = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            templatePath = WebUtils.SystemUrl();
            if (!IsPostBack)
            {
               
                Loadgoiy();
                LoadQuangCaoSP();
                LoadTivi();
                LoadDienThoai();
                LoadLaptop();
            }
        }

        #region Bin data Goi y
        void Loadgoiy()
        {
            var show = from k in dx.Product_tbs
                       where k.goiy == true
                       select k;
            rptGoiy.DataSource = show;
            rptGoiy.DataBind();

        }
        #endregion

        #region Bin data TiVi
        void LoadTivi()
        {
            var show = from k in dx.Product_tbs
                       where k.iCateID == 1
                       select k;
            rptTivi.DataSource = show;
            rptTivi.DataBind();
        }
        #endregion

        #region Bin data Dien Thoai
        void LoadDienThoai()
        {
            var show = from k in dx.Product_tbs
                       where k.iCateID == 3
                       select k;
            rptDienthoai.DataSource = show;
            rptDienthoai.DataBind();

        }
        #endregion

        #region Bin data Laptop-Vitinh
        void LoadLaptop()
        {
            var show = from k in dx.Product_tbs
                       where k.iCateID == 4
                       select k;

            rptLaptop.DataSource = show;
            rptLaptop.DataBind();
        }
        #endregion

        #region Bin data QuangCao
        void LoadQuangCaoSP()
        {
            var QuangCao = from k in dx.GetTable<ListItemQC>()
                           where k.flag == true
                           select k;
            rptQC.DataSource = QuangCao;
            rptQC.DataBind();

        }

        protected void rptQC_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            ListItemQC cat = (ListItemQC)e.Item.DataItem;
            Repeater rptProduct = (Repeater)e.Item.FindControl("rptQuangCao");

            if (rptProduct != null)
            {
                IList<QuangCao> list = getByQC(cat.ID, 3);
                if (list != null && list.Count != 0)
                {
                    rptProduct.DataSource = list;
                    rptProduct.DataBind();
                }
            }
        }

        public IList<QuangCao> getByQC(int catid, int loai)
        {
            try
            {

                var menulst = from k in dx.GetTable<QuangCao>()
                              where k.ListItemID == catid && k.DanhMucID == loai && k.flag == true
                              select k;
                IList<QuangCao> list = new List<QuangCao>();
                foreach (QuangCao pp in menulst)
                {
                    list.Add(pp);

                }
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion
    }
}